package com.drama.api.sys.withdraw.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import com.drama.api.base.Constants;
import com.drama.api.base.Type;
import com.drama.api.company.message.dao.CompanyMessageDao;
import com.drama.api.sys.log.dao.SysLogDao;
import com.drama.api.sys.withdraw.dao.SysCompanyWithdrawDao;
import com.drama.api.sys.withdraw.domain.po.SysCompanyWithdrawPO;
import com.drama.api.sys.withdraw.domain.vo.SysCompanyWithdrawCountVO;
import com.drama.api.sys.withdraw.domain.vo.SysCompanyWithdrawSearchVO;
import com.drama.api.sys.withdraw.domain.vo.SysCompanyWithdrawUpdateVO;
import com.drama.api.sys.withdraw.domain.vo.SysCompanyWithdrawVO;
import com.drama.api.sys.withdraw.service.ISysCompanyWithdrawService;
import com.drama.api.task.CompanyMessageThread;
import com.drama.api.task.SysLogThread;
import com.drama.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
@Slf4j
public class SysCompanyWithdrawServiceImpl implements ISysCompanyWithdrawService {

    @Autowired
    private SysCompanyWithdrawDao withdrawDao;
    @Autowired
    private SysLogDao sysLogDao;
    @Autowired
    private CompanyMessageDao companyMessageDao;

    @Override
    public List<SysCompanyWithdrawVO> getList(SysCompanyWithdrawSearchVO searchVO) {
        return withdrawDao.getList(searchVO);
    }


    @Override
    public SysCompanyWithdrawCountVO get_count() {
        SysCompanyWithdrawCountVO companyWithdrawCountVO=new SysCompanyWithdrawCountVO();
        companyWithdrawCountVO.setPaidMoney(withdrawDao.getCountMoney(2));
        companyWithdrawCountVO.setUnPaidMoney(withdrawDao.getCountMoney(1));
        return companyWithdrawCountVO;
    }

    @Override
    public void complete(SysCompanyWithdrawUpdateVO vo) {
        SysCompanyWithdrawPO companyWithdrawPO=withdrawDao.selectByPrimaryKey(vo.getId());
        if(companyWithdrawPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        if(companyWithdrawPO.getWithdrawState()!=1){
            throw new BusinessException(Type.FAIL.getCode(),"当前状态不能打款！");
        }
        companyWithdrawPO.setWithdrawState(2);
        companyWithdrawPO.setOrderNumber(vo.getOrderNumber());
        companyWithdrawPO.preUpdate();
        int i = withdrawDao.updateByPrimaryKeySelective(companyWithdrawPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
        new SysLogThread(sysLogDao,Constants.SYS_LOG_MODULE[2],"完成打款，提现ID:"+companyWithdrawPO.getId(), (String) StpUtil.getLoginId()).start();

        new CompanyMessageThread(companyMessageDao, companyWithdrawPO.getCompanyId(),"提现结果通知",
                DateUtil.format(companyWithdrawPO.getCreateDate(),"yyyy年MM月dd日 HH:mm:ss")+" 发起"+companyWithdrawPO.getWithdrawAmount()+"元的提现申请平台方已打款，请确认是否到账，详情前往【收益结算】中进行查阅。"
        ,"/withdraw",2,(String) StpUtil.getLoginId()).start();
    }
}